/**
 * 保存最顶一排的数值，进行右列替换；保存右列数值，进行底行替换。循环。
 * @param {number[][]} matrix
 * @return {void} Do not return anything, modify matrix in-place instead.
 */
var rotate = function (matrix) {
    let row = matrix.length,
        column = matrix[0].length,
        curNums = [];

    let [top, bottom, left, right] = [0, row - 1, 0, column - 1]

    while (top < bottom) {
        for (let i = left; i <= right; i++) {
            curNums.push(matrix[top][i])
        }
        for (let i = top; i <= bottom; i++) {
            curNums.push(matrix[i][right])
            matrix[i][right] = curNums.shift()
        }

        // 角落的值已经替换，不需要重复替换
        curNums.shift();
        for (let i = right - 1; i >= left; i--) {
            curNums.push(matrix[bottom][i])
            matrix[bottom][i] = curNums.shift()
        }

        for (let i = bottom - 1; i >= top; i--) {
            curNums.push(matrix[i][left])
            matrix[i][left] = curNums.shift()
        }

        for (let i = left + 1; i <= right - 1; i++) {
            matrix[top][i] = curNums.shift()
        }
        curNums.shift();

        [top, bottom, left, right] = [top + 1, bottom - 1, left + 1, right - 1];
    }

};